package org.jeecg.modules.xxgx.fpgl.service.impl;

import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxf;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxfmx;
import org.jeecg.modules.xxgx.fpgl.mapper.CsXxFpxfmxMapper;
import org.jeecg.modules.xxgx.fpgl.mapper.CsXxFpxfMapper;
import org.jeecg.modules.xxgx.fpgl.service.ICsXxFpxfService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 发票修复
 * @Author: jeecg-boot
 * @Date:   2023-10-25
 * @Version: V1.0
 */
@Service
public class CsXxFpxfServiceImpl extends ServiceImpl<CsXxFpxfMapper, CsXxFpxf> implements ICsXxFpxfService {

	@Autowired
	private CsXxFpxfMapper csXxFpxfMapper;
	@Autowired
	private CsXxFpxfmxMapper csXxFpxfmxMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CsXxFpxf csXxFpxf, List<CsXxFpxfmx> csXxFpxfmxList) {
		csXxFpxfMapper.insert(csXxFpxf);
		if(csXxFpxfmxList!=null && csXxFpxfmxList.size()>0) {
			for(CsXxFpxfmx entity:csXxFpxfmxList) {
				//外键设置
				entity.setFormid(csXxFpxf.getId());
				csXxFpxfmxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CsXxFpxf csXxFpxf,List<CsXxFpxfmx> csXxFpxfmxList) {
		csXxFpxfMapper.updateById(csXxFpxf);
		
		//1.先删除子表数据
		csXxFpxfmxMapper.deleteByMainId(csXxFpxf.getId());
		
		//2.子表数据重新插入
		if(csXxFpxfmxList!=null && csXxFpxfmxList.size()>0) {
			for(CsXxFpxfmx entity:csXxFpxfmxList) {
				//外键设置
				entity.setFormid(csXxFpxf.getId());
				csXxFpxfmxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		csXxFpxfmxMapper.deleteByMainId(id);
		csXxFpxfMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			csXxFpxfmxMapper.deleteByMainId(id.toString());
			csXxFpxfMapper.deleteById(id);
		}
	}
	
}
